/*
 * _______________#########_______________________ 
 * ______________############_____________________ 
 * ______________#############____________________ 
 * _____________##__###########___________________ 
 * ____________###__######_#####__________________ 
 * ____________###_#######___####_________________ 
 * ___________###__##########_####________________ 
 * __________####__###########_####_______________ 
 * ________#####___###########__#####_____________ 
 * _______######___###_########___#####___________ 
 * _______#####___###___########___######_________ 
 * ______######___###__###########___######_______ 
 * _____######___####_##############__######______ 
 * ____#######__#####################_#######_____ 
 * ____#######__##############################____ 
 * ___#######__######_#################_#######___ 
 * ___#######__######_######_#########___######___ 
 * ___#######____##__######___######_____######___ 
 * ___#######________######____#####_____#####____ 
 * ____######________#####_____#####_____####_____ 
 * _____#####________####______#####_____###______ 
 * ______#####______;###________###______#________ 
 * ________##_______####________####______________ 
 * 
 * @Author: ipk518 121206530@qq.com
 * @Date: 2025-01-21 11:46:18
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-02-19 16:58:31
 * @FilePath: /axilite_qspi/src/flash.c
 * @Description: 
 * ************佛祖保佑************
 * Copyright (c) 2025 by etws@quyujiang, All Rights Reserved. 
 */


#include "flash.h"

#include <stdlib.h>

extern const struct flash_driver qspi_flash_driver;


struct flash_device *flash_open_qspi(int data_width)
{
    struct flash_device *fdev;

    fdev = calloc(1, sizeof(struct flash_device));

    if (!fdev)
        return NULL;

    fdev->driver = &qspi_flash_driver;

    fdev->data_width = data_width;

    fdev->ctrl_reg = NULL;

    if (fdev->driver->init(fdev))
    {
        goto err;
    }

    return fdev;

err:
    flash_release(fdev);
    return NULL;
}

void flash_release(struct flash_device *fdev)
{
    if (!fdev)
        return;

    fdev->driver->release(fdev);

    free(fdev);
}

int flash_read(struct flash_device *fdev, size_t addr, size_t len, void *dest)
{
    if (!fdev)
        return -1;

    return fdev->driver->read(fdev, addr, len, dest);
}

int flash_write(struct flash_device *fdev, size_t addr, size_t len, const void *src)
{
    if (!fdev)
        return -1;

    return fdev->driver->write(fdev, addr, len, src);
}

int flash_erase(struct flash_device *fdev, size_t addr, size_t len)
{
    if (!fdev)
        return -1;

    return fdev->driver->erase(fdev, addr, len);
}

int flash_ioctl(struct flash_device *fdev, uint8_t mode, void *msg)
{
    if (!fdev)
        return -1;

    return fdev->driver->ioctl(fdev, mode, msg);
}
